/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SketchFrame.java
 *
 * Created on 05/03/2010, 18:54:46
 */
package imghunter;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author eduardo
 */
public class SketchFrame extends javax.swing.JFrame {

    private BufferedImage sketch;
    private final String sketchName = "sketch.jpg";
    private final String sketchExtension = "jpg";

    private final int LAPIS = 0;
    private final int LINHA = 1;
    private final int RETANGULO = 2;
    private final int FILL_RETANGULO = 3;
    private final int OVAL = 4;
    private final int FILL_OVAL = 5;
    private final int BORRACHA = 6;

    private int mousey;
    private int mousex;
    private int iniX;
    private int iniY;
    private int diametroBorracha;
    private boolean initialDraw;
    private int prevx;
    private int prevy;
    private Color drawColor;
    private Color xorColor = new Color(255,255,255);

    private JLabel lbl;

    /** Creates new form SketchFrame */
    public SketchFrame(JLabel lbl) {
        initComponents();
        this.sketch = new BufferedImage(this.jpnDraw.getWidth(),
                this.jpnDraw.getHeight(), BufferedImage.TYPE_INT_RGB);

        mousey = 0;
        mousex = 0;
        prevx = 0;
        prevy = 0;
        initialDraw = true;
        iniX = 0;
        iniY = 0;
        diametroBorracha = 7;

        this.drawColor = Color.BLACK;

        this.lbl = lbl;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jpnColors = new javax.swing.JPanel(new GridLayout(1,11));
        jbtnUser = new javax.swing.JButton();
        jbtnBlack = new javax.swing.JButton();
        jbtnGrey = new javax.swing.JButton();
        jbtnBlue = new javax.swing.JButton();
        jbtnRed = new javax.swing.JButton();
        jbtnOrange = new javax.swing.JButton();
        jbtnPurple = new javax.swing.JButton();
        jbtnPink = new javax.swing.JButton();
        jbtnYellow = new javax.swing.JButton();
        jbtnGreen = new javax.swing.JButton();
        lblCoord = new javax.swing.JLabel();
        lblColors = new javax.swing.JLabel();
        jpnControles = new javax.swing.JPanel();
        jbtnLapis = new javax.swing.JButton();
        jbtnLinha = new javax.swing.JButton();
        jbtnRect = new javax.swing.JButton();
        jbtnFillRect = new javax.swing.JButton();
        jbtnOval = new javax.swing.JButton();
        jbtnFillOval = new javax.swing.JButton();
        jbtnBorracha = new javax.swing.JButton();
        jbtnLimpar = new javax.swing.JButton();
        lblFerrEscolhida = new javax.swing.JLabel();
        jpnDraw = new javax.swing.JPanel();
        lblFerramentas = new javax.swing.JLabel();
        SketchMenuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        importMenuItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(imghunter.ImgHunterApp.class).getContext().getResourceMap(SketchFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("Form"); // NOI18N

        mainPanel.setName("mainPanel"); // NOI18N

        jpnColors.setName("jpnColors"); // NOI18N
        jpnColors.setPreferredSize(new java.awt.Dimension(629, 47));
        jpnColors.setRequestFocusEnabled(false);

        jbtnUser.setText(resourceMap.getString("jbtnUser.text")); // NOI18N
        jbtnUser.setName("jbtnUser"); // NOI18N
        jbtnUser.setPreferredSize(new java.awt.Dimension(20, 20));

        jbtnBlack.setBackground(resourceMap.getColor("jbtnBlack.background")); // NOI18N
        jbtnBlack.setName("jbtnBlack"); // NOI18N
        jbtnBlack.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnBlack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnBlackActionPerformed(evt);
            }
        });

        jbtnGrey.setBackground(resourceMap.getColor("jbtnGrey.background")); // NOI18N
        jbtnGrey.setName("jbtnGrey"); // NOI18N
        jbtnGrey.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnGrey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGreyActionPerformed(evt);
            }
        });

        jbtnBlue.setBackground(resourceMap.getColor("jbtnBlue.background")); // NOI18N
        jbtnBlue.setName("jbtnBlue"); // NOI18N
        jbtnBlue.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnBlue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnBlueActionPerformed(evt);
            }
        });

        jbtnRed.setBackground(resourceMap.getColor("jbtnRed.background")); // NOI18N
        jbtnRed.setName("jbtnRed"); // NOI18N
        jbtnRed.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnRed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRedActionPerformed(evt);
            }
        });

        jbtnOrange.setBackground(resourceMap.getColor("jbtnOrange.background")); // NOI18N
        jbtnOrange.setName("jbtnOrange"); // NOI18N
        jbtnOrange.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnOrange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnOrangeActionPerformed(evt);
            }
        });

        jbtnPurple.setBackground(resourceMap.getColor("jbtnPurple.background")); // NOI18N
        jbtnPurple.setName("jbtnPurple"); // NOI18N
        jbtnPurple.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnPurple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPurpleActionPerformed(evt);
            }
        });

        jbtnPink.setBackground(resourceMap.getColor("jbtnPink.background")); // NOI18N
        jbtnPink.setName("jbtnPink"); // NOI18N
        jbtnPink.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnPink.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPinkActionPerformed(evt);
            }
        });

        jbtnYellow.setBackground(resourceMap.getColor("jbtnYellow.background")); // NOI18N
        jbtnYellow.setName("jbtnYellow"); // NOI18N
        jbtnYellow.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnYellow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnYellowActionPerformed(evt);
            }
        });

        jbtnGreen.setBackground(resourceMap.getColor("jbtnGreen.background")); // NOI18N
        jbtnGreen.setName("jbtnGreen"); // NOI18N
        jbtnGreen.setPreferredSize(new java.awt.Dimension(20, 20));
        jbtnGreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGreenActionPerformed(evt);
            }
        });

        lblCoord.setText(resourceMap.getString("lblCoord.text")); // NOI18N
        lblCoord.setName("lblCoord"); // NOI18N

        org.jdesktop.layout.GroupLayout jpnColorsLayout = new org.jdesktop.layout.GroupLayout(jpnColors);
        jpnColors.setLayout(jpnColorsLayout);
        jpnColorsLayout.setHorizontalGroup(
            jpnColorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jpnColorsLayout.createSequentialGroup()
                .addContainerGap()
                .add(jbtnUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(29, 29, 29)
                .add(jbtnBlack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnGrey, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnBlue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnRed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnOrange, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnPurple, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnPink, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnGreen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jbtnYellow, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 468, Short.MAX_VALUE)
                .add(lblCoord)
                .addContainerGap())
        );
        jpnColorsLayout.setVerticalGroup(
            jpnColorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jpnColorsLayout.createSequentialGroup()
                .addContainerGap()
                .add(jpnColorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(lblCoord)
                    .add(jpnColorsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jbtnUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnBlack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnGrey, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnBlue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnRed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnOrange, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnPurple, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnPink, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnGreen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jbtnYellow, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblColors.setText(resourceMap.getString("lblColors.text")); // NOI18N
        lblColors.setName("lblColors"); // NOI18N

        jpnControles.setName("jpnControles"); // NOI18N

        jbtnLapis.setText(resourceMap.getString("jbtnLapis.text")); // NOI18N
        jbtnLapis.setName("jbtnLapis"); // NOI18N
        jbtnLapis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLapisActionPerformed(evt);
            }
        });

        jbtnLinha.setText(resourceMap.getString("jbtnLinha.text")); // NOI18N
        jbtnLinha.setName("jbtnLinha"); // NOI18N
        jbtnLinha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLinhaActionPerformed(evt);
            }
        });

        jbtnRect.setText(resourceMap.getString("jbtnRect.text")); // NOI18N
        jbtnRect.setName("jbtnRect"); // NOI18N
        jbtnRect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRectActionPerformed(evt);
            }
        });

        jbtnFillRect.setText(resourceMap.getString("jbtnFillRect.text")); // NOI18N
        jbtnFillRect.setName("jbtnFillRect"); // NOI18N

        jbtnOval.setText(resourceMap.getString("jbtnOval.text")); // NOI18N
        jbtnOval.setName("jbtnOval"); // NOI18N

        jbtnFillOval.setText(resourceMap.getString("jbtnFillOval.text")); // NOI18N
        jbtnFillOval.setName("jbtnFillOval"); // NOI18N

        jbtnBorracha.setText(resourceMap.getString("jbtnBorracha.text")); // NOI18N
        jbtnBorracha.setName("jbtnBorracha"); // NOI18N
        jbtnBorracha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnBorrachaActionPerformed(evt);
            }
        });

        jbtnLimpar.setText(resourceMap.getString("jbtnLimpar.text")); // NOI18N
        jbtnLimpar.setName("jbtnLimpar"); // NOI18N
        jbtnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLimparActionPerformed(evt);
            }
        });

        lblFerrEscolhida.setText(resourceMap.getString("lblFerrEscolhida.text")); // NOI18N
        lblFerrEscolhida.setName("lblFerrEscolhida"); // NOI18N

        org.jdesktop.layout.GroupLayout jpnControlesLayout = new org.jdesktop.layout.GroupLayout(jpnControles);
        jpnControles.setLayout(jpnControlesLayout);
        jpnControlesLayout.setHorizontalGroup(
            jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jpnControlesLayout.createSequentialGroup()
                .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jpnControlesLayout.createSequentialGroup()
                        .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(jbtnBorracha, 0, 0, Short.MAX_VALUE)
                            .add(jbtnLapis, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jbtnRect, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jbtnOval, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jbtnLinha, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jbtnFillRect, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jbtnFillOval, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jbtnLimpar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)))
                    .add(jpnControlesLayout.createSequentialGroup()
                        .add(48, 48, 48)
                        .add(lblFerrEscolhida)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpnControlesLayout.setVerticalGroup(
            jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jpnControlesLayout.createSequentialGroup()
                .addContainerGap()
                .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jbtnLapis)
                    .add(jbtnLinha))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jbtnRect)
                    .add(jbtnFillRect))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jbtnOval)
                    .add(jbtnFillOval))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jpnControlesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jbtnLimpar)
                    .add(jbtnBorracha))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(lblFerrEscolhida)
                .addContainerGap(130, Short.MAX_VALUE))
        );

        jpnDraw.setBackground(resourceMap.getColor("jpnDraw.background")); // NOI18N
        jpnDraw.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jpnDraw.setName("jpnDraw"); // NOI18N
        jpnDraw.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jpnDrawMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jpnDrawMouseReleased(evt);
            }
        });
        jpnDraw.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jpnDrawMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jpnDrawMouseMoved(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jpnDrawLayout = new org.jdesktop.layout.GroupLayout(jpnDraw);
        jpnDraw.setLayout(jpnDrawLayout);
        jpnDrawLayout.setHorizontalGroup(
            jpnDrawLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 592, Short.MAX_VALUE)
        );
        jpnDrawLayout.setVerticalGroup(
            jpnDrawLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 378, Short.MAX_VALUE)
        );

        lblFerramentas.setText(resourceMap.getString("lblFerramentas.text")); // NOI18N
        lblFerramentas.setName("lblFerramentas"); // NOI18N

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jpnColors, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 769, Short.MAX_VALUE)
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jpnControles, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(lblColors))
                                .add(18, 18, 18)
                                .add(jpnDraw, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(40, 40, 40)
                        .add(lblFerramentas)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(mainPanelLayout.createSequentialGroup()
                                .add(lblFerramentas)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jpnControles, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jpnDraw, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(18, 18, 18)
                        .add(jpnColors, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                        .add(lblColors)
                        .add(50, 50, 50))))
        );

        SketchMenuBar.setName("SketchMenuBar"); // NOI18N

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        importMenuItem.setText(resourceMap.getString("importMenuItem.text")); // NOI18N
        importMenuItem.setName("importMenuItem"); // NOI18N
        importMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(importMenuItem);

        SketchMenuBar.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N
        SketchMenuBar.add(jMenu2);

        setJMenuBar(SketchMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnBlackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnBlackActionPerformed
        this.jbtnUser.setBackground(this.jbtnBlack.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnBlackActionPerformed

    private void jbtnGreyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGreyActionPerformed
        this.jbtnUser.setBackground(this.jbtnGrey.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnGreyActionPerformed

    private void jbtnBlueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnBlueActionPerformed
        this.jbtnUser.setBackground(this.jbtnBlue.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnBlueActionPerformed

    private void jbtnRedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRedActionPerformed
        this.jbtnUser.setBackground(this.jbtnRed.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnRedActionPerformed

    private void jbtnOrangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnOrangeActionPerformed
        this.jbtnUser.setBackground(this.jbtnOrange.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnOrangeActionPerformed

    private void jbtnPurpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPurpleActionPerformed
        this.jbtnUser.setBackground(this.jbtnPurple.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnPurpleActionPerformed

    private void jbtnPinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPinkActionPerformed
        this.jbtnUser.setBackground(this.jbtnPink.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnPinkActionPerformed

    private void jbtnGreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGreenActionPerformed
        this.jbtnUser.setBackground(this.jbtnGreen.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnGreenActionPerformed

    private void jbtnYellowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnYellowActionPerformed
        this.jbtnUser.setBackground(this.jbtnYellow.getBackground());
        drawColor = this.jbtnUser.getBackground();
    }//GEN-LAST:event_jbtnYellowActionPerformed

    private void jpnDrawMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jpnDrawMouseMoved
        updateCoordText(evt.getX(), evt.getY());
    }//GEN-LAST:event_jpnDrawMouseMoved

    private void jbtnLapisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLapisActionPerformed
        this.lblFerrEscolhida.setText(jbtnLapis.getText());
    }//GEN-LAST:event_jbtnLapisActionPerformed

    private void jpnDrawMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jpnDrawMouseClicked
        updateCoordText(evt.getX(), evt.getY());
    }//GEN-LAST:event_jpnDrawMouseClicked

    private void jpnDrawMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jpnDrawMouseReleased

        this.updateCoordText(evt.getX(), evt.getY());
        int ferrSel = getFerramentaSelecionada(this.lblFerrEscolhida.getText());


        switch (ferrSel) {
            case LAPIS:
                releaseLapis(evt);
                break;

            case LINHA:
                releaseLinha(evt);
                break;

            case RETANGULO:
                releaseRect(evt);
                break;

            case FILL_RETANGULO:
                break;

            case OVAL:
                break;

            case FILL_OVAL:
                break;

            case BORRACHA:
                releaseBorracha(evt);
                break;
        }


    }//GEN-LAST:event_jpnDrawMouseReleased

    private void jpnDrawMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jpnDrawMouseDragged
        this.updateCoordText(evt.getX(), evt.getY());

        int ferrSel = getFerramentaSelecionada(this.lblFerrEscolhida.getText());

        switch (ferrSel) {
            case LAPIS:
                drawLapis(evt);
                break;

            case LINHA:
                drawLinhas(evt);
                break;

            case RETANGULO:
                drawRect(evt);
                break;

            case FILL_RETANGULO:
                break;

            case OVAL:
                break;

            case FILL_OVAL:
                break;

            case BORRACHA:
                drawBorracha(evt);
                break;
        }
    }//GEN-LAST:event_jpnDrawMouseDragged

    private void jbtnLinhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLinhaActionPerformed
        this.lblFerrEscolhida.setText(this.jbtnLinha.getText());
    }//GEN-LAST:event_jbtnLinhaActionPerformed

    private void jbtnRectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRectActionPerformed
        this.lblFerrEscolhida.setText(this.jbtnRect.getText());
}//GEN-LAST:event_jbtnRectActionPerformed

    private void jbtnBorrachaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnBorrachaActionPerformed
        this.lblFerrEscolhida.setText(this.jbtnBorracha.getText());
}//GEN-LAST:event_jbtnBorrachaActionPerformed

    private void jbtnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLimparActionPerformed
        Graphics g = this.jpnDraw.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, jpnDraw.getWidth(), jpnDraw.getHeight());
    }//GEN-LAST:event_jbtnLimparActionPerformed

    private void importMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importMenuItemActionPerformed

 //       Graphics g = this.sketch.createGraphics();
 //       this.jpnDraw.paint(g);

        try {
           ImageIO.write(sketch, sketchExtension, new File(sketchName));

        }catch(Exception E)
        {
            System.err.print("Erro ao importar imagem de rascunho: " + E.getMessage());
            JOptionPane.showMessageDialog(this, null, "Erro ao importar Rascunho", 0);
        }

       // this.setVisible(false);

    }//GEN-LAST:event_importMenuItemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar SketchMenuBar;
    private javax.swing.JMenuItem importMenuItem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JButton jbtnBlack;
    private javax.swing.JButton jbtnBlue;
    private javax.swing.JButton jbtnBorracha;
    private javax.swing.JButton jbtnFillOval;
    private javax.swing.JButton jbtnFillRect;
    private javax.swing.JButton jbtnGreen;
    private javax.swing.JButton jbtnGrey;
    private javax.swing.JButton jbtnLapis;
    private javax.swing.JButton jbtnLimpar;
    private javax.swing.JButton jbtnLinha;
    private javax.swing.JButton jbtnOrange;
    private javax.swing.JButton jbtnOval;
    private javax.swing.JButton jbtnPink;
    private javax.swing.JButton jbtnPurple;
    private javax.swing.JButton jbtnRect;
    private javax.swing.JButton jbtnRed;
    private javax.swing.JButton jbtnUser;
    private javax.swing.JButton jbtnYellow;
    private javax.swing.JPanel jpnColors;
    private javax.swing.JPanel jpnControles;
    private javax.swing.JPanel jpnDraw;
    private javax.swing.JLabel lblColors;
    private javax.swing.JLabel lblCoord;
    private javax.swing.JLabel lblFerrEscolhida;
    private javax.swing.JLabel lblFerramentas;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables

    private void updateCoordText(int x, int y) {
        String xCoor = "";
        String yCoor = "";

        if (x < 0) {
            xCoor = "0";
        } else {
            xCoor = String.valueOf(x);
        }

        if (y < 0) {
            xCoor = "0";
        } else {
            yCoor = String.valueOf(y);
        }

        lblCoord.setText("[" + xCoor + "],  [" + yCoor + "]");
    }

    private int getFerramentaSelecionada(String text) {

        if (text.equals(jbtnLapis.getText())) {
            return LAPIS;
        } else if (text.equals(jbtnLinha.getText())) {
            return LINHA;
        } else if (text.equals(jbtnRect.getText())) {
            return RETANGULO;
        } else if (text.equals(jbtnFillRect.getText())) {
            return FILL_RETANGULO;
        } else if (text.equals(jbtnOval.getText())) {
            return OVAL;
        } else if (text.equals(jbtnFillOval.getText())) {
            return FILL_OVAL;
        } else {
            return BORRACHA;
        }
    }

    public boolean mouseHasMoved(MouseEvent e) {
        return (mousex != e.getX() || mousey != e.getY());
    }

    private void drawLapis(MouseEvent e) {

        Graphics g = this.jpnDraw.getGraphics();
        g.setColor(this.drawColor);


        if (this.initialDraw) {
            this.initialDraw = false;

            mousex = e.getX();
            mousey = e.getY();
            prevx = mousex;
            prevy = mousey;
            g.drawLine(prevx, prevy, mousex, mousey);
        }

        if (mouseHasMoved(e)) {

            mousex = e.getX();
            mousey = e.getY();

            g.drawLine(prevx, prevy, mousex, mousey);

            prevx = mousex;
            prevy = mousey;

        }
    }

    private void drawLinhas(MouseEvent e) {
        Graphics g = jpnDraw.getGraphics();
        g.setColor(this.drawColor);


        if (initialDraw) {
            initialDraw = false;
            g.setXORMode(xorColor);

            mousex = e.getX();
            mousey = e.getY();
            prevx = mousex;
            prevy = mousey;
            g.drawLine(prevx, prevy, mousex, mousey);

        }

        if (mouseHasMoved(e)) {

            g.setXORMode(xorColor);
            g.drawLine(prevx, prevy, mousex, mousey);

            mousex = e.getX();
            mousey = e.getY();

            g.drawLine(prevx, prevy, mousex, mousey);
        }
    }

    private void drawRect(MouseEvent evt) {

        Graphics g = jpnDraw.getGraphics();
        g.setColor(this.drawColor);

        if (this.initialDraw) {
            this.initialDraw = false;

            this.mousex = evt.getX();
            this.mousey = evt.getY();
            this.iniX = evt.getX();
            this.iniY = evt.getY();
            this.prevx = mousex;
            this.prevy = mousey;

        }

        /*
        Make sure that the mouse has actually
        moved from its previous position.
         */
        if (mouseHasMoved(evt)) {

            g.setXORMode(xorColor);
            g.drawLine(prevx, prevy, mousex, mousey);

            mousex = evt.getX();
            mousey = evt.getY();

            g.drawLine(prevx, prevy, mousex, mousey);
        }
    }

    private void releaseLapis(MouseEvent evt) {
        this.initialDraw = true;
    }

    private void releaseLinha(MouseEvent evt) {
        this.initialDraw = true;
    }

    private void releaseRect(MouseEvent evt) {
        this.initialDraw = true;
        Graphics g = jpnDraw.getGraphics();
        g.setColor(drawColor);
        g.drawRect(iniX, iniY, 0, 0);

    }

    private void drawBorracha(MouseEvent evt) {

        Graphics g = this.jpnDraw.getGraphics();

        if (initialDraw) {
            this.initialDraw = false;

            this.mousex = this.jpnDraw.getX();
            this.mousey = this.jpnDraw.getY();
            this.prevx = mousex;
            this.prevy = mousey;

            g.setColor(Color.white);
            g.fillRect(mousex - diametroBorracha, mousey - diametroBorracha,
                    diametroBorracha * 2, diametroBorracha * 2);
            g.setColor(Color.black);
            g.drawRect(mousex - diametroBorracha, mousey - diametroBorracha,
                    diametroBorracha * 2, diametroBorracha * 2);
            prevx = mousex;
            prevy = mousey;

        }

        if (mouseHasMoved(evt)) {

            g.setColor(Color.white);
            g.drawRect(prevx - diametroBorracha, prevy - diametroBorracha,
                    diametroBorracha * 2, diametroBorracha * 2);

            /* Get current mouse coordinates */
            mousex = evt.getX();
            mousey = evt.getY();

            /* Draw eraser block to panel */
            g.setColor(Color.white);
            g.fillRect(mousex - diametroBorracha, mousey - diametroBorracha,
                    diametroBorracha * 2, diametroBorracha * 2);
            g.setColor(Color.black);
            g.drawRect(mousex - diametroBorracha, mousey - diametroBorracha,
                    diametroBorracha * 2, diametroBorracha * 2);
            prevx = mousex;
            prevy = mousey;

        }
    }

    private void releaseBorracha(MouseEvent evt) {
        initialDraw = true;
        Graphics g = jpnDraw.getGraphics();
        g.setColor(Color.white);
        g.drawRect(mousex - diametroBorracha, mousey - diametroBorracha,
                diametroBorracha * 2, diametroBorracha * 2);
    }
}
